package com.bbex.mapper;

import com.bbex.entity.WithdrawLog;
import com.bbex.sql.build.WithdrawLogSqlBuild;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface WithdrawLogDao {

    @InsertProvider(type = WithdrawLogSqlBuild.class, method = "insert")
    void insert(WithdrawLog withdrawLog);

    @SelectProvider(type = WithdrawLogSqlBuild.class, method = "findById")
    WithdrawLog findById(String id);

    @Select("select id,create_date as createDate,update_date as updateDate,user_id as userId,coin_id as coinId,coin_symbol as coinSymbol," +
            "coin_type as coinType,status,confirm_status as confirmStatus,address,tx_id as txId,volume,real_volume as realVolume,fee,remark," +
            "tag,chain_type as chainType,audit_reason as auditReason,version,create_by as createBy,update_by as updateBy from js_plat_user_withdraw_log where id = #{id}")
    WithdrawLog selectdById(String id);

    @UpdateProvider(type = WithdrawLogSqlBuild.class, method = "updateById")
    long updateById(WithdrawLog withdrawLog);

    @Update("update js_plat_user_withdraw_log set status=#{status},version=version+1 where id=#{id} and status =#{sourceStatus} and version=#{version}")
    long updateStatusByIdAndStatus(@Param("id") String id, @Param("status") String status, @Param("sourceStatus") String sourceStatus, @Param("version") Long version);

    @Select("select " + WithdrawLogSqlBuild.columns + " from js_plat_user_withdraw_log where coin_id = #{coinId} and user_id = #{userId} and status != 8 order by create_date desc")
    List<WithdrawLog> findWithdrawListByUserIdAndCoinId(@Param("userId") String userId, @Param("coinId") String coinId);

    @Select("select " + WithdrawLogSqlBuild.columns + " from js_plat_user_withdraw_log where user_id = #{userId} and status != 8 order by create_date desc")
    List<WithdrawLog> findWithdrawListByUserId(String userId);

    @Select("select count(volume) from js_plat_user_withdraw_log where user_id = #{userId} and coin_id = #{coinId} and status in (0,1,3) ")
    long countDayVolumeByUserIdAndCoinIdAndStatus(@Param("userId") String userId, @Param("coinId") String coinId);

    @Select("select count(*) from js_plat_user_withdraw_log where user_id = #{userId} and address = #{address} and status = 3")
    long countAddressByStatusAndUserId(@Param("userId") String userId, @Param("address") String address);


    @Update("update js_plat_user_withdraw_log set status=#{status},version=version+1 where id=#{id} and version=#{version}")
    long updateStatusById(@Param("status") String status, @Param("id") String id, @Param("version") Long version);

}
